﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Basic4Net.OAWeb.Base;
using System.Data;
using Basic4Net.Utility.Common;

namespace Basic4Net.OAWeb.Module_Attendance
{
    public partial class AttendanceHistory : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                yearbind();
                monthbind();
                classbind();
                listbind();
            }
        }

        private void yearbind()
        {
            this.ddl_Year.DataValueField = "FYear";
            this.ddl_Year.DataTextField = "FYear";
            this.ddl_Year.DataSource = (new BusinessRules.Attendance()).YearList();
            this.ddl_Year.DataBind();
        }

        private void monthbind()
        {
            int[] month = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
            this.ddl_Month.DataSource = month;
            this.ddl_Month.DataBind();
        }

        private void classbind()
        {
            this.ddl_FType.DataValueField = "RID";
            this.ddl_FType.DataTextField = "PostName";
            this.ddl_FType.DataSource = (new BusinessRules.Post()).FindAll(SchoolMark);
            this.ddl_FType.DataBind();
            ListItem item1 = new ListItem();
            item1.Value = "0";
            item1.Text = "尚未分类";
            this.ddl_FType.Items.Add(item1);
            ListItem item2 = new ListItem();
            item2.Value = "-1";
            item2.Text = "----全部----";
            this.ddl_FType.Items.Add(item2);
            this.ddl_FType.SelectedValue = "-1";
        }

        private void listbind()
        {
            DataTable dt = new DataTable();
            Common.IListToTable(dt, new BusinessRules.ListAttendance());
            IList<BusinessRules.ListAttendance> mylist = (new BusinessRules.Attendance()).FindALL(int.Parse(this.ddl_Year.SelectedValue), int.Parse(this.ddl_Month.SelectedValue), int.Parse(this.ddl_FType.SelectedValue));
            for (int i = 0; i < mylist.Count; i++)
            {
                DataRow row = dt.NewRow();
                row["TeacherID"] = mylist[i].TeacherID;
                row["TeachCode"] = mylist[i].TeachCode;
                row["TeachName"] = mylist[i].TeachName;
                row["PostName"] = mylist[i].PostName != null ? mylist[i].PostName : "";
                row["Late"] = mylist[i].Late;
                row["Early"] = mylist[i].Early;
                row["Truant"] = mylist[i].Truant;
                row["Absenteeism"] = mylist[i].Absenteeism;
                row["Sick"] = mylist[i].Sick;
                row["Leave"] = mylist[i].Leave;
                row["Other"] = mylist[i].Other;
                row["OtherContect"] = mylist[i].OtherContect;
                dt.Rows.Add(row);
            }

            //------------------------------------------------------------------------分页处理开始......
            int PageNow = 0;
            DataView dv = dt.DefaultView;
            System.Web.UI.WebControls.PagedDataSource ps = new PagedDataSource();
            ps.DataSource = dv;
            ps.AllowPaging = true;
            ps.PageSize = 20;
            if (this.txt_Go.Text.ToString() == "")
                this.txt_Go.Text = "1";
            if (this.txt_Go.Text.ToString() == "0")
                this.txt_Go.Text = "1";
            if (Convert.ToInt32(this.txt_Go.Text.ToString()) >= ps.PageCount)
                this.txt_Go.Text = ps.PageCount.ToString();
            PageNow = Convert.ToInt32(this.txt_Go.Text.ToString()) - 1;
            //this.lbn_Up.Enabled = true;
            //this.lbn_Next.Enabled = true;
            //if (PageNow == 0)
            //{
            //    this.lbn_Up.Enabled = false;
            //}
            //if (PageNow == ps.PageCount - 1)
            //{
            //    this.lbn_Next.Enabled = false;
            //}
            ps.CurrentPageIndex = Convert.ToInt32(this.txt_Go.Text.ToString()) - 1;
            //-------------------------------------------------------------------------分页处理结束

            this.rpt_TeacherType.DataSource = ps;
            this.rpt_TeacherType.DataBind();

            //------------------------------------------------------------------------记录总数、当前页数、总页数处理开始......
            //this.lbl_SumCount.Text = ds.Tables[0].Rows.Count.ToString();
            this.lbl_ToPage.Text = this.txt_Go.Text.ToString();
            this.lbl_PageCount.Text = ps.PageCount.ToString();

            //------------------------------------------------------------------------------记录总数、当前页数、总页数处理结束
        }

        protected void btnBind_Click(object sender, EventArgs e)
        {
            listbind();
        }
    }
}